#include "CSampleManager.h"

extern const char* SAMPLE_NAMES[];
extern int maxSampleNum;
extern void errorMessage(WCHAR* str);

CSampleManager::CSampleManager()
{
	voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16,
		ALLEGRO_CHANNEL_CONF_2);
	mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32,
		ALLEGRO_CHANNEL_CONF_2);
	al_attach_mixer_to_voice(mixer, voice);
	for (int i = 0; SAMPLE_NAMES[i]; i++)
	{
		arrSample.push_back(al_load_sample(SAMPLE_NAMES[i]));
		if (!arrSample[i])
		{
			errorMessage(L"can't open the sample");
		}
	}
	sample = al_create_sample_instance(NULL);
}

CSampleManager::~CSampleManager()
{
	for (vector<ALLEGRO_SAMPLE*>::iterator itor = arrSample.begin(); itor != arrSample.end(); itor++)
	{
		al_destroy_sample(*itor);
	}
	arrSample.clear();
}

void CSampleManager::playSample(int i)
{
	if (i > maxSampleNum)
	{
		return;
	}
	al_set_sample(sample, arrSample[i]);
	al_attach_sample_instance_to_mixer(sample, mixer);
	al_set_sample_instance_playmode(sample, ALLEGRO_PLAYMODE_ONCE);
	al_play_sample_instance(sample);
}
